var regeneratorRuntime = require("../../../lib/js/runtime-module.js")
import Serv from './active_list_serv';
import formatDate from '../../../utils/util2.js'
var cf = require("../../../utils/config2");
var employee = wx.getStorageSync('employee');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex:null,
    isActiveData:true,
    pageNo: 1,
    pageSize: 999,
    total:0,
    type:''
    
  },
  onLoad: function(option) {
    let data = option;
    let title = null;
    let projectName = null;
    if(data.type=='active'){
      title = '楼盘活动';
      projectName = 'activity'
      this.getHouseActivity(projectName);
    }
    if(data.type=='message'){
      title='楼盘资讯'
      projectName = 'news'
      this.getHouseActivity(projectName);
    }
    this.setData({
      title,type:data.type,projectName
    })
    
  },

  //获取楼盘活动
  getHouseActivity: async function (name) {
    if(employee){
       let { pageSize,pageNo,isActiveData,activityList,total,projectName} = this.data,
           that = this,
           phone = employee.phone,
           activityData = [];
           isActiveData = true;
           projectName = name||projectName
       wx.request({
        url: `${cf.default.dataCenter}zmzj/${projectName}/${phone}/${pageNo}/${pageSize}`,
        header: {
          'content-type': 'application/json',
          'token':wx.getStorageSync('authName')
        },
        success: function (res) {
          if(res.data.data&&res.data.data.list.length>0){
            total =  res.data.data.totalCount;
            activityData = res.data.data.list && res.data.data.list.filter((item, idx) => {
              if(item.endDate){
                return item.endDate && formatDate.contrastDate(item.endDate);
              }else{
                return item;
              }
            })
            activityData&&activityData.sort((a, b) => { return a['updateTime'] < b['updateTime'] ? 1 : -1 });
            if( activityData.length==0 ) {//过滤时间过期数据
               isActiveData = false;
              }
            else if( pageNo > 1 ){//分页
              activityList = activityList.concat(activityData);
            }else{
              activityList = activityData
            }
          }else{
            isActiveData = false;
          }
          that.setData({
            activityList,isActiveData,total
          })
         
      }
     })
   
    }
  },
   //页面相关事件处理函数--页面上拉触底事件的
  /* onReachBottom() {
    let { pageNo, activityList, total } = this.data;
    if (activityList.length < total) {
        pageNo += 1;
        this.setData({ pageNo })
        this.getHouseActivity();
    }

},*/
  //选择活动
  selectActive(e){
    let data = e.currentTarget.dataset;
    this.setData({
        activeIndex:data.key
    });
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面
    prevPage.setData({
      sendCoupon: data.item
    });
    wx.showModal({
      title: '提示',
      content: `确定发送此${this.data.title}？`,
      success (res) {
        if (res.confirm) {
          prevPage.handleSendCouponMsg(data.style);
             wx.navigateBack({
               delta: 1
           });
        } else if (res.cancel) {
         
        }
      }
    })
    
    
  }
  
})